﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using PolePosition.v3.Common;
using ProjectCommon2.Wcf.Behaviors;
using System.ServiceModel;
using System.ServiceModel.Channels;
using PolePosition.v3.EntityLibrary;
using System.Data;

namespace PolePosition.v3.ServiceProxy
{
    public class UtilityService
    {
        #region Static Reference to the UtilityService

        private static string m_ServiceEndpointFileName = "Utility.svc";

        internal static UtilityServiceReference.UtilityClient GetServiceClient()
        {
            Binding _Binding;
            EndpointAddress _EndPointAddress;

            _Binding = Setup.Binding;
            _EndPointAddress = Setup.GetEndPointAddress(m_ServiceEndpointFileName);

            UtilityServiceReference.UtilityClient _ServiceClient;
            _ServiceClient = new UtilityServiceReference.UtilityClient(_Binding, _EndPointAddress);

            _ServiceClient.Endpoint.Behaviors.Add(new AddUserCreatedModifiedClientHeaderBehavior());
            _ServiceClient.Endpoint.Behaviors.Add(new PushContextClientBehavior());

            // Update the data contract serializer for the operations to avoid max limit of objects in graph issue
            MaxDataContractSerializerOperationBehavior.ReplaceDataContactSerializer(_ServiceClient.Endpoint.Contract.Operations);

            //insert additional logic here?
            return _ServiceClient;
        }

        #endregion

        #region Get Project Data for Grids

        public static List<VendorVisitXMissionStructuresPoleEntity> GetXmissionGpsProjectPoleData(int projectId, int? pageSize, int? pageNumber, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext, out int totalRecords)
        {
            //DateTime startTime = DateTime.Now;
            //DateTime stopTime;
            //TimeSpan _ts;

            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionGpsProjectPoleData(projectId, pageSize, pageNumber, searchBy, searchBytext);

                //stopTime = DateTime.Now;
                //_ts = stopTime - startTime;;
                //System.Diagnostics.Debug.WriteLine("Retrieval Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            //convert dataset to library objects
         
            //ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresImageEntity> _images;
            //_images = new ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresImageEntity>(_Return.Tables[1]);

            var _LINQCollection = (from tableRow in _Return.Tables[0].AsEnumerable()
                                   select new VendorVisitXMissionStructuresPoleEntity(tableRow)
                                   {
		
                                       Vendor_Visit_TypeFK = tableRow.Field<int?>("Vendor_Visit_TypeFK"),
                                       VisitDate = tableRow.Field<DateTime?>("VisitDate"),
                                       Vendor_ContactFK = tableRow.Field<int?>("Vendor_ContactFK"),
                                       VendorFK = tableRow.Field<int?>("VendorFK"),
                                       VendorName = tableRow.Field<string>("VendorName"),
                                       VendorContactName = tableRow.Field<string>("VendorContactName"),
                                       //VendorVisitXMissionStructuresImageCollection
                                       //   = new ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresImageEntity>(_images.Where(JoinImages => JoinImages.Vendor_Visit_XMissionStructures_PoleFK == tableRow.Field<int>("Vendor_Visit_XMissionStructures_PoleId")).ToArray())

                                   }); 

            //stopTime = DateTime.Now;
            //_ts = stopTime - startTime;;
            //System.Diagnostics.Debug.WriteLine("LINQ Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

            List<VendorVisitXMissionStructuresPoleEntity> _EntityList = _LINQCollection.ToList();

            //stopTime = DateTime.Now;
            //_ts = stopTime - startTime;;
            //System.Diagnostics.Debug.WriteLine("Final Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

            return _EntityList;
        }

        public static DataSet GetXmissionGpsProjectPoleExcelData(int projectId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionGpsProjectPoleExcelData(projectId, searchBy, searchBytext);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static List<VendorVisitXMissionStructureEntity> GetXmissionGpsProjectStructureData(int projectId, int? pageSize, int? pageNumber, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext, out int totalRecords)
        {
            //DateTime startTime = DateTime.Now;
            //DateTime stopTime;
            //TimeSpan _ts;
            
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionGpsProjectStructureData(projectId, pageSize, pageNumber, searchBy, searchBytext);

                //stopTime = DateTime.Now;
                //_ts = stopTime - startTime;;
                //System.Diagnostics.Debug.WriteLine("Retrieval Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;
            
            //convert dataset to library objects
            ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresPoleEntity> _poles;
            _poles = new ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresPoleEntity>(_Return.Tables[1]);

            //ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresImageEntity> _images;
            //_images = new ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresImageEntity>(_Return.Tables[2]);

            var _LINQCollection = (from table1Row in _Return.Tables[0].AsEnumerable()
                              select new VendorVisitXMissionStructureEntity(table1Row)
                              {
                                  VendorVisitXMissionStructuresPoleCollection
                                     = new ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresPoleEntity>(_poles.Where(JoinPoles => JoinPoles.Vendor_Visit_XMissionStructureFK == table1Row.Field<int>("Vendor_Visit_XMissionStructureId")).ToArray())
                                     ,

                                  //VendorVisitXMissionStructuresImageCollection
                                  //    = new ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresImageEntity>(_images.Where(JoinImages => JoinImages.Vendor_Visit_XMissionStructureFK == table1Row.Field<int>("Vendor_Visit_XMissionStructureId")).ToArray())
                              }); //.Take(100);

            //stopTime = DateTime.Now;
            //_ts = stopTime - startTime;;
            //System.Diagnostics.Debug.WriteLine("LINQ Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

            List<VendorVisitXMissionStructureEntity> _EntityList = _LINQCollection.ToList();

            //stopTime = DateTime.Now;
            //_ts = stopTime - startTime;;
            //System.Diagnostics.Debug.WriteLine("Final Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

            return _EntityList;
        }

        public static List<VendorInspVisitXmissionStructuresPoleEntity> GetXmissionInspectionProjectPoleData(int projectId, int? pageSize, int? pageNumber, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext, out int totalRecords)
        {
            //DateTime startTime = DateTime.Now;
            //DateTime stopTime;
            //TimeSpan _ts;

            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionInspectionProjectPoleData(projectId, pageSize, pageNumber, searchBy, searchBytext);

                //stopTime = DateTime.Now;
                //_ts = stopTime - startTime;;
                //System.Diagnostics.Debug.WriteLine("Retrieval Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            //convert dataset to library objects
            ProjectCommon2.Generics.EntityCollection<VendorInspVisitXmissionStructuresResultEntity> _InspectionResults;
            _InspectionResults = new ProjectCommon2.Generics.EntityCollection<VendorInspVisitXmissionStructuresResultEntity>(_Return.Tables[1]);

            var _LINQStructureCollection = (from tableRow in _Return.Tables[0].AsEnumerable()
                                            select new VendorInspVisitXmissionStructuresPoleEntity(tableRow)
                                   {
                                        InspectionTypeFk = tableRow.Field<int?>("Vendor_InspVisit_XmissionStructures_Inspection_TypeFk"),
                                        ResultTypeFk = tableRow.Field<int?>("Vendor_InspVisit_XmissionStructures_ResultTypeFk"),
                                        Electrical = tableRow.Field<bool?>("bElectrical"),
                                        TopTypeDisplay = tableRow.Field<string>("TopTypeDisplay"),
                                        NoteDisplay = tableRow.Field<string>("NoteDisplay"),
                                        Vendor_Visit_TypeFK = tableRow.Field<int?>("Vendor_Visit_TypeFK"),
                                        VendorName = tableRow.Field<string>("VendorName"),
                                        VendorContactName = tableRow.Field<string>("VendorContactName"),
                                        Comments = tableRow.Field<string>("Comments"),
                                        VendorInspVisitXmissionStructuresResultCollection
                                          = new ProjectCommon2.Generics.EntityCollection<VendorInspVisitXmissionStructuresResultEntity>(_InspectionResults.Where(JoinResults => JoinResults.Vendor_InspVisit_XMissionStructure_PoleFk == tableRow.Field<int>(VendorInspVisitXmissionStructuresPoleEnumerations.Columns.Vendor_InspVisit_XMissionStructure_PoleId.ToString())).ToArray())
                                   }); 
            //stopTime = DateTime.Now;
            //_ts = stopTime - startTime;;
            //System.Diagnostics.Debug.WriteLine("LINQ Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

            List<VendorInspVisitXmissionStructuresPoleEntity> _EntityList = _LINQStructureCollection.ToList();

            //stopTime = DateTime.Now;
            //_ts = stopTime - startTime;;
            //System.Diagnostics.Debug.WriteLine("Final Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

            return _EntityList;
        }

        public static DataSet GetXmissionInspectionProjectPoleExcelData(int projectId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionInspectionProjectPoleExcelData(projectId, searchBy, searchBytext);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static List<VendorInspVisitXMissionStructureEntity> GetXmissionInspectionProjectStructureData(int projectId, int? pageSize, int? pageNumber, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext, out int totalRecords)
        {
            //DateTime startTime = DateTime.Now;
            //DateTime stopTime;
            //TimeSpan _ts;

            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionInspectionProjectStructureData(projectId, pageSize, pageNumber, searchBy, searchBytext);

                //stopTime = DateTime.Now;
                //_ts = stopTime - startTime;;
                //System.Diagnostics.Debug.WriteLine("Retrieval Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            //convert dataset to library objects
            List<VendorInspVisitXmissionStructuresPoleEntity> _Poles;
            _Poles = (from tableRow in _Return.Tables[1].AsEnumerable()
                      select new VendorInspVisitXmissionStructuresPoleEntity(tableRow)
                      {
                          InspectionTypeFk = tableRow.Field<int?>("Vendor_InspVisit_XmissionStructures_Inspection_TypeFk"),
                          ResultTypeFk = tableRow.Field<int?>("Vendor_InspVisit_XmissionStructures_ResultTypeFk"),
                          Electrical = tableRow.Field<bool?>("bElectrical"),
                          //NoteFk = tableRow.Field<int?>("Vendor_InspVisit_XmissionStructures_NoteFk")
                          NoteDisplay = tableRow.Field<string>("NoteDisplay")
                      }).ToList();


            var _LINQStructureCollection = (from tableRow in _Return.Tables[0].AsEnumerable()
                                            select new VendorInspVisitXMissionStructureEntity(tableRow)
                                            {
                                                VendorInspVisitXmissionStructuresPoleCollection
                                                   = new ProjectCommon2.Generics.EntityCollection<VendorInspVisitXmissionStructuresPoleEntity>(_Poles.Where(JoinPoles => JoinPoles.Vendor_InspVisit_XMissionStructureFk == tableRow.Field<int>("Vendor_InspVisit_XMissionStructureId")).ToArray())
                                            });
            //stopTime = DateTime.Now;
            //_ts = stopTime - startTime;;
            //System.Diagnostics.Debug.WriteLine("LINQ Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

            List<VendorInspVisitXMissionStructureEntity> _EntityList = _LINQStructureCollection.ToList();

            //stopTime = DateTime.Now;
            //_ts = stopTime - startTime;;
            //System.Diagnostics.Debug.WriteLine("Final Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

            return _EntityList;
        }

        public static List<VendorDistributionPoleEntity> GetDistributionInspectionProjectData(int projectId, int? pageSize, int? pageNumber, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext, out int totalRecords)
        {
            //DateTime startTime = DateTime.Now;
            //DateTime stopTime;
            //TimeSpan _ts;

            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetDistributionInspectionProjectData(projectId, pageSize, pageNumber, searchBy, searchBytext);

                //stopTime = DateTime.Now;
                //_ts = stopTime - startTime;;
                //System.Diagnostics.Debug.WriteLine("Retrieval Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            //convert dataset to library objects

            ProjectCommon2.Generics.EntityCollection<VendorDistributionInspectionResultEntity> _Results;
            _Results = new ProjectCommon2.Generics.EntityCollection<VendorDistributionInspectionResultEntity>(_Return.Tables[1]);

            ProjectCommon2.Generics.EntityCollection<VendorDistributionMeterEntity> _Meters;
            _Meters = new ProjectCommon2.Generics.EntityCollection<VendorDistributionMeterEntity>(_Return.Tables[2]);

            ProjectCommon2.Generics.EntityCollection<VendorDistributionTransformerEntity> _Transformers;
            _Transformers = new ProjectCommon2.Generics.EntityCollection<VendorDistributionTransformerEntity>(_Return.Tables[3]);

            ProjectCommon2.Generics.EntityCollection<VendorDistributionSectionalizerEntity> _Sectionalizers;
            _Sectionalizers = new ProjectCommon2.Generics.EntityCollection<VendorDistributionSectionalizerEntity>(_Return.Tables[4]);

            ProjectCommon2.Generics.EntityCollection<VendorDistributionRecloserEntity> _Reclosers;
            _Reclosers = new ProjectCommon2.Generics.EntityCollection<VendorDistributionRecloserEntity>(_Return.Tables[5]);

            var _LINQCollection = (from tableRow in _Return.Tables[0].AsEnumerable()
                                   select new VendorDistributionPoleEntity(tableRow)
                                   {
                                       DistributionTypeFk = tableRow.Field<int?>("Vendor_Distribution_TypeFk"),
                                       ResultTypeFk = tableRow.Field<int?>("Vendor_Distribution_ResultTypeFk"),
                                       Electrical = tableRow.Field<bool?>("bElectrical"),
                                       Comments = tableRow.Field<string>("Comments"),
                                       TopTypeDisplay = tableRow.Field<string>("TopTypeDisplay"),
                                       NoteDisplay = tableRow.Field<string>("NoteDisplay"),
                                       PhaseDisplay = tableRow.Field<string>("PhaseDisplay"),
                                       VendorName = tableRow.Field<string>("VendorName"),
                                       VendorContactName = tableRow.Field<string>("VendorContactName"),
                                       VendorDistributionMeterCollection
                                            = new ProjectCommon2.Generics.EntityCollection<VendorDistributionMeterEntity>(_Meters.Where(JoinMeters => JoinMeters.Vendor_Distribution_PoleFk == tableRow.Field<int>("Vendor_Distribution_Poles_PoleId")).ToArray()),
                                       VendorDistributionTransformerCollection  = new ProjectCommon2.Generics.EntityCollection<VendorDistributionTransformerEntity>(_Transformers.Where(JoinTransformers => JoinTransformers.Vendor_Distribution_PoleFk == tableRow.Field<int>("Vendor_Distribution_Poles_PoleId")).ToArray()),
                                       VendorDistributionSectionalizerCollection = new ProjectCommon2.Generics.EntityCollection<VendorDistributionSectionalizerEntity>(_Sectionalizers.Where(JoinSectionalizers => JoinSectionalizers.Vendor_Distribution_PoleFk == tableRow.Field<int>("Vendor_Distribution_Poles_PoleId")).ToArray()),
                                       VendorDistributionRecloserCollection = new ProjectCommon2.Generics.EntityCollection<VendorDistributionRecloserEntity>(_Reclosers.Where(JoinReclosers => JoinReclosers.Vendor_Distribution_PoleFk == tableRow.Field<int>("Vendor_Distribution_Poles_PoleId")).ToArray()),
                                       VendorDistributionInspectionResultCollection = new ProjectCommon2.Generics.EntityCollection<VendorDistributionInspectionResultEntity>(_Results.Where(JoinResults => JoinResults.Vendor_Distribution_PoleFk == tableRow.Field<int>("Vendor_Distribution_Poles_PoleId")).ToArray())
                                   });

            //stopTime = DateTime.Now;
            //_ts = stopTime - startTime;;
            //System.Diagnostics.Debug.WriteLine("LINQ Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

            List<VendorDistributionPoleEntity> _EntityList = _LINQCollection.ToList();

            //stopTime = DateTime.Now;
            //_ts = stopTime - startTime;;
            //System.Diagnostics.Debug.WriteLine("Final Duration (seconds):" + _ts.Duration().TotalSeconds.ToString());

            return _EntityList;
        }

        public static DataSet GetDistributionInspectionProjectExcelData(int projectId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetDistributionInspectionProjectExcelData(projectId, searchBy, searchBytext);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        #endregion

        #region Get Asset Data for Grids

        public static List<UtilityAssetXMissionStructuresPoleEntity> GetXmissionAssetData(int utilityId, int? pageSize, int? pageNumber, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext, out int totalRecords)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionAssetData(utilityId, pageSize, pageNumber, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;


            ProjectCommon2.Generics.EntityCollection<UtilityAssetXMissionStructuresPoleEntity> _Collection;
            _Collection = new ProjectCommon2.Generics.EntityCollection<UtilityAssetXMissionStructuresPoleEntity>(_Return.Tables[0]);
            
            return _Collection.ToList<UtilityAssetXMissionStructuresPoleEntity>();
        }

        public static DataSet GetXmissionAssetExcelData(int utilityId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionAssetExcelData(utilityId, searchBy, searchBytext);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static List<UtilityAssetDistributionEntity> GetDistributionAssetData(int utilityId, int? pageSize, int? pageNumber, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext, out int totalRecords)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetDistributionAssetData(utilityId, pageSize, pageNumber, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            //convert dataset to library objects
            var _LINQStructureCollection = (from tableRow in _Return.Tables[0].AsEnumerable()
                                            select new UtilityAssetDistributionEntity(tableRow)
                                            {
                                                TopTypeDisplay = tableRow.Field<string>("TopTypeDisplay"),
                                                PhaseDisplay = tableRow.Field<string>("PhaseDisplay"),
                                                LastVisit = tableRow.Field<string>("LastVisit"),
                                                Number = tableRow.Field<string>("Number")
                                            });

            List<UtilityAssetDistributionEntity> _EntityList = _LINQStructureCollection.ToList();
            return _EntityList;
        }

        public static DataSet GetDistributionAssetExcelData(int utilityId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetDistributionAssetExcelData(utilityId, searchBy, searchBytext);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static List<UtilityAssetMeterEntity> GetMeterAssetData(int utilityId, int? pageSize, int? pageNumber, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext, out int totalRecords)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetMeterAssetData(utilityId, pageSize, pageNumber, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            ProjectCommon2.Generics.EntityCollection<UtilityAssetMeterEntity> _Meters;
            _Meters = new ProjectCommon2.Generics.EntityCollection<UtilityAssetMeterEntity>(_Return.Tables[0]);

            return _Meters.ToList();
        }

        public static List<UtilityAssetTransformerEntity> GetTransformerAssetData(int utilityProjectId, int? pageSize, int? pageNumber, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext, out int totalRecords)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetTransformerAssetData(utilityProjectId, pageSize, pageNumber, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            ProjectCommon2.Generics.EntityCollection<UtilityAssetTransformerEntity> _Transformers;
            _Transformers = new ProjectCommon2.Generics.EntityCollection<UtilityAssetTransformerEntity>(_Return.Tables[0]);

            return _Transformers.ToList();
        }

        public static DataSet GetMeterAssetExcelData(int utilityId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetMeterAssetExcelData(utilityId, searchBy, searchBytext);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static DataSet GetTransformerAssetExcelData(int utilityId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetTransformerAssetExcelData(utilityId, searchBy, searchBytext);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }
        
        #endregion

        #region Get Project Data for Visits to Assets Grids

        public static DataTable GetDistributionInspectionToAssetData(int projectId, int? pageSize, int? pageNumber, int? feets, out int totalRecords)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetDistributionInspectionToAssetData(projectId, pageSize, pageNumber, feets);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return != null && _Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            return _Return.Tables[0];
        }

        public static DataTable GetDistributionMeterToAssetData(int projectId, int? pageSize, int? pageNumber, int? feets, out int totalRecords)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetDistributionMeterToAssetData(projectId, pageSize, pageNumber, feets);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return != null && _Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            return _Return.Tables[0];
        }

        public static DataTable GetXMissionInspectionToAssetData(int projectId, int? pageSize, int? pageNumber, int? feets, out int totalRecords)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXMissionInspectionToAssetData(projectId, pageSize, pageNumber, feets);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return != null && _Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            return _Return.Tables[0];
        }

        public static DataTable GetXMissionVisitToAssetData(int projectId, int? pageSize, int? pageNumber, int? feets, out int totalRecords)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXMissionVisitToAssetData(projectId, pageSize, pageNumber, feets);

                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            if (_Return != null && _Return.Tables[0].Rows.Count > 0)
                totalRecords = _Return.Tables[0].Rows[0].Field<int>("TotalCount");
            else
                totalRecords = 0;

            return _Return.Tables[0];
        }

        #endregion

        #region Project KML for Grids

        public static string GetXmissionGpsProjectPoleKML(int projectId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionGpsProjectPoleKML(projectId, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
            return _Return;
        }

        public static string GetXmissionGpsProjectStructureKML(int projectId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionGpsProjectStructureKML(projectId, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
            return _Return;

        }

        public static string GetXmissionInspectionProjectPoleKML(int projectId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionInspectionProjectPoleKML(projectId, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
            return _Return;

        }

        public static string GetXmissionInspectionProjectStructureKML(int projectId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionInspectionProjectStructureKML(projectId, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
            return _Return;

        }

        public static string GetDistributionInspectionProjectKML(int projectId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetDistributionInspectionProjectKML(projectId, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
            return _Return;
        }

        #endregion

        public static string TestKml()
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.TestKml();
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
            return _Return;
        }

        #region Asset KML for Grids

        public static string GetXmissionAssetKML(int utilityId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetXmissionAssetKML(utilityId, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
            return _Return;
        }

        public static string GetDistributionAssetKML(int utilityId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetDistributionAssetKML(utilityId, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
            return _Return;
        }

        public static string GetMeterAssetKML(int utilityId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetMeterAssetKML(utilityId, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
            return _Return;
        }

        public static string GetTransformerAssetKML(int utilityId, Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetTransformerAssetKML(utilityId, searchBy, searchBytext);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
            return _Return;
        }



        #endregion

        #region WorkQueue KML for Grids

        public static string GetWorkqueueKML(string transmissionInspectionVisitsList, string distributionInspectionVisitsList)
        {
             string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetWorkqueueKML(transmissionInspectionVisitsList, distributionInspectionVisitsList);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        #endregion

        #region Get Specific Data Xmission Inspection

        public static ProjectCommon2.Generics.EntityCollection<VendorInspVisitXmissionStructuresResultsXNoteEntity> GetXmissionInspectionProjectNotesForResult(int resultId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorInspVisitXmissionStructuresResultsXNoteEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorInspVisitXmissionStructuresResultsXNoteEntity>(_Client.GetXmissionInspectionProjectNotesForResult(resultId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static ProjectCommon2.Generics.EntityCollection<VendorInspVisitXmissionStructuresImageEntity> GetXmissionInspectionProjectPoleImages(int poleId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorInspVisitXmissionStructuresImageEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorInspVisitXmissionStructuresImageEntity>(_Client.GetXmissionInspectionProjectPoleImages(poleId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }
        
        #endregion

        #region Get Specific Data Xmission Gps

        public static ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresImageEntity> GetXmissionGpsProjectPoleImages(int poleId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresImageEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorVisitXMissionStructuresImageEntity>(_Client.GetXmissionGpsProjectPoleImages(poleId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }
        
        #endregion

        #region Get Specific Data Distribution Inspection

        public static ProjectCommon2.Generics.EntityCollection<VendorDistributionResultEntity> GetDistributionInspectionProjectNotesForResult(int poleId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorDistributionResultEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorDistributionResultEntity>(_Client.GetDistributionInspectionProjectNotesForResult(poleId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXMeterEntity> GetDistributionInspectionProjectNotesForMeter(int meterId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXMeterEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXMeterEntity>(_Client.GetDistributionInspectionProjectNotesForMeter(meterId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXTransformerEntity> GetDistributionInspectionProjectNotesForTransformer(int transformerId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXTransformerEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXTransformerEntity>(_Client.GetDistributionInspectionProjectNotesForTransformer(transformerId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXSectionalizerEntity> GetDistributionInspectionProjectNotesForSectionalizer(int sectionalizerId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXSectionalizerEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXSectionalizerEntity>(_Client.GetDistributionInspectionProjectNotesForSectionalizer(sectionalizerId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXRecloserEntity> GetDistributionInspectionProjectNotesForRecloser(int recloserId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXRecloserEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXRecloserEntity>(_Client.GetDistributionInspectionProjectNotesForRecloser(recloserId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static ProjectCommon2.Generics.EntityCollection<VendorDistributionTopsXPoleEntity> GetDistributionInspectionProjectTopsForPole(int poleId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorDistributionTopsXPoleEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorDistributionTopsXPoleEntity>(_Client.GetDistributionInspectionProjectTopsForPole(poleId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static ProjectCommon2.Generics.EntityCollection<VendorDistributionPhasesXPoleEntity> GetDistributionInspectionProjectPhasesForPole(int poleId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorDistributionPhasesXPoleEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorDistributionPhasesXPoleEntity>(_Client.GetDistributionInspectionProjectPhasesForPole(poleId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static ProjectCommon2.Generics.EntityCollection<VendorDistributionImageEntity> GetDistributionInspectionProjectPoleImages(int poleId)
        {
            ProjectCommon2.Generics.EntityCollection<VendorDistributionImageEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<VendorDistributionImageEntity>(_Client.GetDistributionInspectionProjectPoleImages(poleId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        #endregion

        #region Get Specific Data Xmission Structures

        public static ProjectCommon2.Generics.EntityCollection<UtilityAssetXMissionStructuresImageEntity> GetXmissionAssetPoleImages(int poleId)
        {
            ProjectCommon2.Generics.EntityCollection<UtilityAssetXMissionStructuresImageEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<UtilityAssetXMissionStructuresImageEntity>(_Client.GetXmissionAssetPoleImages(poleId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        #endregion

        #region Get Specific Data Distribution Structures

        public static string GetDistributionMetersAssetKmlString(string meterIdList)
        {
            string _Return = string.Empty;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetDistributionMetersAssetKmlString(meterIdList);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;            
        }

        public static string GetDistributionTransformersAssetKmlString(string meterIdList)
        {
            string _Return = string.Empty;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetDistributionTransformerssAssetKmlString(meterIdList);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static ProjectCommon2.Generics.EntityCollection<UtilityAssetDistributionsImageEntity> GetDistributionAssetPoleImages(int poleId)
        {
            ProjectCommon2.Generics.EntityCollection<UtilityAssetDistributionsImageEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<UtilityAssetDistributionsImageEntity>(_Client.GetDistributionAssetPoleImages(poleId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static ProjectCommon2.Generics.EntityCollection<UtilityAssetMetersImageEntity> GetMeterAssetImages(int meterId)
        {
            ProjectCommon2.Generics.EntityCollection<UtilityAssetMetersImageEntity> _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = new ProjectCommon2.Generics.EntityCollection<UtilityAssetMetersImageEntity>(_Client.GetMeterAssetImages(meterId));
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static int[] DistributionAssetsValidateIds(int utilityId, int[] assetIds)
        {
            int[] _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.ValidateDistributionAssetId(utilityId, assetIds);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static int[] DistributionMeterAssetsValidateIds(int utilityId, int[] assetIds)
        {
            int[] _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.ValidateMeterAssetId(utilityId, assetIds);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static int[] XMissionAssetsValidateIds(int utilityId, int[] assetIds)
        {
            int[] _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.ValidateInspectionAssetId(utilityId, assetIds);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static void DistributionAssetsUpdateParents(string xml)
        {
            int[] _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Client.UpdateParentsByXml(xml);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
        }

        #endregion

        #region QuickSearch

        public static string QuickSearch(int? utilityId, string value, Enumerations.DataTypes type)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.QuickSearch(utilityId, value, type);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        #endregion

        public static void CreateDistributionAssetsFromInspectionVisits(int ? vendorProjectFk, XElement xml)
        {
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Client.CreateDistributionAssetsFromInspectionVisits(vendorProjectFk, xml);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
        }

        public static void CreateDistributionAssetsFromMeterVisits(int ? vendorProjectFk, XElement xml)
        {
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Client.CreateDistributionAssetsFromMeterVisits(vendorProjectFk, xml);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
        }

        public static void CreateXMissionAssetsFromInspectionVisits(int? vendorProjectFk, XElement xml)
        {
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Client.CreateXMissionAssetsFromInspection(vendorProjectFk, xml);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
        }

        public static void CreateXMissionAssetsFromVisits(int? vendorProjectFk, XElement xml)
        {
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Client.CreateXMissionAssetsFromVisits(vendorProjectFk, xml);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }
        }

        public static string GetUtilityKMLString(int? utilityId, int? userId)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetUtilityKmlString(utilityId, userId);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;            
        }

        public static string GetMapSearchCriteriaKMLString(
            string xMissionText,
            string distributionText,
            string xMissionGpsProjectList, 
            string xMissionInspectionProjectList, 
            string distributionInspectionProjectList, 
            string distributionGpsProjectList, 
            bool? xMissionAssets, 
            bool? distributionAssets, 
            bool? meterAssets,
            bool? transformerAssets,
            int? utilityFk, 
            string mapList,
            string sectionList, 
            string xMissionKvList, 
            string xMissionTopsList, 
            string xMissionSpeciesList, 
            string xMissionTreatmentList, 
            string xMissionClassList, 
            string xMissionAgeList, 
            string distributionPhaseList, 
            string distributionTopsList, 
            string distributionSpeciesList, 
            string distributionTreatmentList, 
            string distributionClassList, 
            string distributionAgeList, 
            string meterNotesList, 
            bool? electicalProblem, 
            string inspectionResultList, 
            string inspectionTypeList, 
            string inspectionVendorList, 
            string inspectionCompanyList, 
            string inspectionNoteList)
        {
            string _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetMapSearchCriteriaKMLString(xMissionText, distributionText, xMissionGpsProjectList, xMissionInspectionProjectList, distributionInspectionProjectList, distributionGpsProjectList, xMissionAssets, distributionAssets, meterAssets, transformerAssets, utilityFk, mapList, sectionList, xMissionKvList, xMissionTopsList, xMissionSpeciesList, xMissionTreatmentList, xMissionClassList, xMissionAgeList, distributionPhaseList, distributionTopsList, distributionSpeciesList, distributionTreatmentList, distributionClassList, distributionAgeList, meterNotesList, electicalProblem, inspectionResultList, inspectionTypeList, inspectionVendorList, inspectionCompanyList, inspectionNoteList);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static DataSet GetWorkQueueData(int utilityId)
        {
            DataSet _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetWorkQueueData(utilityId);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

          
            return _Return;
        }

        #region Various Save Methods

        #region Work Queue Saves

        public static EntityLibrary.UtilityAssetXMissionStructurePolesCrossRemedyVendorInspectionVisitEntity SaveXmissionWorkqueue(EntityLibrary.UtilityAssetXMissionStructurePolesCrossRemedyVendorInspectionVisitEntity remedy)
        {
            EntityLibrary.UtilityAssetXMissionStructurePolesCrossRemedyVendorInspectionVisitEntity _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.SaveXmissionWorkqueue(remedy);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static EntityLibrary.VendorDistributionPoleRemedyEntity SaveDistributionWorkqueue(EntityLibrary.VendorDistributionPoleRemedyEntity remedy)
        {
            EntityLibrary.VendorDistributionPoleRemedyEntity _Return = null;
            UtilityServiceReference.UtilityClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.SaveDistributionWorkqueue(remedy);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        //public static EntityLibrary. SaveMeterWorkqueue(EntityLibrary. remedy)
        //{


        //}

        #endregion

        #region Assets

        #endregion

        #region Visits (Inspection/Gps)

        #endregion

        #endregion
    }
}
